<?php defined('SYSPATH') OR die('No direct access allowed.');

class Comment_Controller extends Forum_Controller {

public $auth_required = TRUE;

	  public function __construct() {
			// все действия над комментами только через POST! Исключение - перенаправление после добавления
      parent::__construct();
			($this->input->post() OR message::has_custom()) OR Event::run('system.404');
      $this->template->set_global('basic_href', Router::uri('forum', array('controller'=>'comment'))."/");
    }

		public function add() {
			$this->history = FALSE;
			if ($data = $olddata = $this->input->post())
			{
				$comment = ORM::factory('forum_comment');
				if ($comment->validate($data))
				{
					$comment->post_id = intval($data['post_id']);
					$comment->category_id = intval($data['category_id']);
					$comment->author_id = $this->user->id;
					$comment->created = time();
					Event::run('forum.before.create.comment', $comment);
					$comment->save();
					Event::run('forum.after.create.comment', $comment);
					message::add(Kohana::lang('comment.add.result_ok'), 'success');
					url::redirect($this->get_referrer()."#comment_".$comment->id);
				}
				else {
					/*if (Captcha::instance()->invalid_count() > Kohana::config('cms.captcha_max_tries'))
						Event::run('cms.captcha.ban');*/
					message::add_validation($data->errors('comment'));
					message::add($olddata, 'custom');
					//url::redirect(Router::$current_uri);
				}
			}
			// it is redirect with saved validation errors
			$data = message::custom();
			$post = ORM::factory('forum_post', intval($data['post_id']));
			$content = new View('forum/blocks/comment_form');
			$content->action = 'add';
			$content->post = $post;
			$content->editor = (Editor::factory('comment')->set_value($data['text'])->render(FALSE, TRUE));
			$this->template->content = $content;
			$this->template->head['title'] = Kohana::lang('comment.add.title');
			$this->template->nav->breadcrumb = $post->category->get_parents(TRUE);
		}

		public function edit() {
			$this->history = FALSE;
			$data = $this->input->post();
			if (!$data OR (count($data)==0)) $data = message::custom();
			if (!$data) {
				message::add(Kohana::lang('comment.edit.no_data'), 'error');
				Event::run('system.404');
			}
			$olddata = $data;
			$id = intval($data['id']);
			$comment = ORM::factory('forum_comment', $id);
			if (!$comment->loaded) {
				message::add(Kohana::lang('comment.edit.not_found'), 'error');
				Event::run('system.404');
			}
			if (isset($data['text'])) {
				if ($comment->validate($data))
				{
					Event::run('forum.before.update.comment', $comment);
					$comment->save();
					Event::run('forum.after.update.comment', $comment);
					message::add(Kohana::lang('comment.edit.result_ok'), 'error');
					url::redirect($this->get_referrer());
				}
				else {
					//$comment->text = $data['text'];
					message::add_validation($data->errors('comment'));
					$this->keep_referrer();
					url::redirect(url::current());
				}
			}
			$post = $comment->post;
			$content = new View('forum/blocks/comment_form');
			$content->action = 'edit';
			$content->post = $post;
			$content->comment = $comment;
			$content->editor = (Editor::factory('comment')->set_value($comment->text)->render(FALSE, TRUE));
			$this->template->content = $content;
			$this->template->head['title'] = Kohana::lang('comment.edit.title');
			$this->template->nav->breadcrumb = $post->category->get_parents(TRUE);
		}

		public function index() {
			echo 'test';
		}

		public function delete() {
			$this->history = FALSE;
			$id = $this->input->post('id');
			$comment = ORM::factory('forum_comment', $id);
			$comment->loaded OR Event::run('system.404');
			$post = $comment->post_id;
			Event::run('forum.before.delete.comment', $comment);
			if ($comment->delete()) {
				Event::run('forum.after.delete.comment', $comment);
				message::add(Kohana::lang('comment.delete.result_ok'), 'success');
				url::redirect($this->session->get(Kohana::config('referrer_key')));
			}
			else {
				message::add(Kohana::lang('comment.delete.result_error'), 'error');
				url::redirect($this->get_referrer());
			}
		}

		
}